package com.atguigu.boot;



import ch.qos.logback.core.db.DBHelper;
import com.atguigu.WorldController;
import com.atguigu.boot.bean.Car;
import com.atguigu.boot.bean.Pet;
import com.atguigu.boot.bean.User;
import com.atguigu.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties;
import org.springframework.cache.interceptor.CacheAspectSupport;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;

/**
 * 主程序类;主配置类
 * @SpringBootApplication：这是一个SpringBoot应用
 */

@SpringBootApplication
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan("com.atguigu.boot")
public class MainApplication {

    @Bean
    public WorldController worldController(){
        return new WorldController();
    }

    public static void main(String[] args) throws IOException {

        // String property = System.getProperty("spring.boot.enableautoconfiguration");
        // System.out.println("property:"+property);
        //

        //1、返回我们IOC容器
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        String property1 = run.getEnvironment().getProperty("spring.boot.enableautoconfiguration");
        System.out.println("property1:"+property1);

        Car bean2 = run.getBean(Car.class);
        System.out.println("bean2 = " + bean2);

        //2、查看容器里面的组件
        System.out.println("/////////////////////////////");
        String[] names = run.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println(name);
        }
        System.out.println("/////////////////////////////");

        //3、获取容器中组件的数量
        int beanDefinitionCount = run.getBeanDefinitionCount();
        System.out.println(beanDefinitionCount);

        String[] beanNamesForType = run.getBeanNamesForType(CacheAspectSupport.class);
        System.out.println("======"+beanNamesForType.length);

        //4、从容器中获取组件
        String[] beanNamesForType1 = run.getBeanNamesForType(WebMvcProperties.class);
        System.out.println("======"+beanNamesForType1.length);

        //5. 容器中的组件默认是单实例的
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("组件："+(tom01 == tom02));

        //6、com.atguigu.boot.config.MyConfig$$EnhancerBySpringCGLIB$$51f1e1ca@1654a892
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);

        //如果@Configuration(proxyBeanMethods = true)代理对象调用方法。SpringBoot总会检查这个组件是否在容器中有。
        //保持组件单实例
        User user = bean.user01();
        User user1 = bean.user01();
        System.out.println("user == user1: "+(user == user1));

        // 配置类使用Full模式(表示配置类中各个组件之间可能有依赖关系，这个时候使用@Configuration(proxyBeanMethods=true))
        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);
        System.out.println("用户的宠物："+(user01.getPet() == tom));

        //5、获取组件
        String[] beanNamesForType2 = run.getBeanNamesForType(User.class);
        System.out.println("======");
        for (String s : beanNamesForType2) {
            System.out.println(s);
        }

        DBHelper bean1 = run.getBean(DBHelper.class);
        System.out.println(bean1);

        //boolean tom = run.containsBean("tom");
        //System.out.println("容器中Tom组件："+tom);
        //
        //boolean user01 = run.containsBean("user01");
        //System.out.println("容器中user01组件："+user01);
        //
        //boolean tom22 = run.containsBean("tom22");
        //System.out.println("容器中tom22组件："+tom22);
        //
        //
        //boolean haha = run.containsBean("haha");
        //boolean hehe = run.containsBean("hehe");
        //System.out.println("haha："+haha);
        //System.out.println("hehe："+hehe);

    }
}
